package com.whz.annontateddemo;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * @Author: sinosoft
 * @Description:
 * @Date: Create in 16:19 2021/5/17
 * @Modified By:
 */
@Slf4j
public class RunTests4 {
    public static void main(String[] args) throws ClassNotFoundException {
        int tests = 0;
        int passed = 0;
        Class<?> clazz = Class.forName("com.whz.annontateddemo.Sample2");
        for (Method m : clazz.getDeclaredMethods()) {
            if(m.isAnnotationPresent(ExceptionTest4.class)
                    || m.isAnnotationPresent(ExceptionTestContainer.class)) {
                tests ++;
                try {
                    m.invoke(null);
                    log.info("方法 {} 不包含异常", m);
                } catch (Throwable wrappedExc) {
                    Throwable exc = wrappedExc.getCause();
                    int oldPassed = passed;
                    ExceptionTest4[] excTests = m.getAnnotationsByType(ExceptionTest4.class);
                    for (ExceptionTest4 excTest : excTests) {
                        if(excTest.value().isInstance(exc)) {
                            passed ++;
                            break;
                        }
                    }
                    if(passed == oldPassed) {
                        log.info("Test注解修饰的方法 {} 运行失败", m, exc);
                    }
                }
            }
        }
        log.info("共 {} 个方法抛出指定异常", passed);
    }
}
